﻿'use strict';

/* ****************************************************************
BillingServiceProxy:
+ Events:
    + after
    + before
    + failed
    + success
**************************************************************** */

define([
    'webServiceProxy',
], function (WebServiceProxy) {
    var Class = WebServiceProxy.extend({
        defaults: _.extend(
            {}, //Add empty object to not extend base classs defaults.
            WebServiceProxy.prototype.defaults,
            {
                url: 'http://localhost/wcf/billing/',
            }
        ),

        addCatalogItem: function (data, options) {
            if (!data) data = {};

            if (data.Properties) data.Properties = this.objToHash(data.Properties);

            return this.request(
                'catalogs/addItem',
                JSON.stringify(data), 
                _.extend(options, { type: 'PUT', })
            );
        },
        adjustProductsPrice: function (data, options) {
            if (!data) data = {};

            return this.request(
                'products/adjustPrice',
                JSON.stringify(data),
                _.extend(options, { type: 'POST', })
            );
        },
        //bill: function (data, options) {
        //    return this.request(
        //        'bill', 
        //        JSON.stringify(data), 
        //        _.extend(options, { type: 'POST', })
        //    );
        //},
        getCatalog: function (data, options) {
            var fn_success;

            //Override success method to parse response data.
            if (!options) options = {};

            var that = this,
                fn_optSuccess = options.success,
                fn_success = function (resp) {
                    var parsedResp = that.parseResponse(resp);

                    if (fn_optSuccess)
                        fn_optSuccess(parsedResp);
                };

            return this.request(
                sprintf(
                    'catalogs/%s',
                    data.name
                ),
                null, 
                _.extend(
                    options,
                    {
                        type: 'GET',
                        success: fn_success,
                    }
                )
            );
        },
        updateCatalogItem: function (data, options) {
            if (!data) data = {};

            if (data.Keys) data.Keys = this.objToHash(data.Keys);
            if (data.Properties) data.Properties = this.objToHash(data.Properties);

            return this.request(
                'catalogs/updateItem',
                JSON.stringify(data), 
                _.extend(options, { type: 'POST', })
            );
        },
    });

    return Class;
});